#include "Stdafx.h"
#include "afxwinappex.h"
#include "WinMFCSystem.h"
#include "AppWinMFC.h"
#include "MainFrm.h"
#include "SplashScreen.h"
#include <mmsystem.h>
#include "ChildView.h"
// CSceneBuilderApp
#include "Appmodul.h"

BEGIN_MESSAGE_MAP(CSceneBuilderApp, CWinAppEx)
	ON_COMMAND(ID_APP_ABOUT, &CSceneBuilderApp::OnAppAbout)
END_MESSAGE_MAP()

int FPSLIST[12] = {1000,200,100,50,33,25,20,10,5,3,2,1};

// The one and only CSceneBuilderApp object
CSceneBuilderApp theApp;

//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

	// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

	// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};
//------------------------------------------------------------------------------------
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}
//------------------------------------------------------------------------------------
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}
//------------------------------------------------------------------------------------
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------



//------------------------------------------------------------------------------------
// CSceneBuilderApp construction
//------------------------------------------------------------------------------------
CSceneBuilderApp::CSceneBuilderApp()
{
	m_bHiColorIcons = TRUE;
	m_System = 0;
	m_LastTime = 0;
	m_FPSID = 0;
	m_FPSTimer = 99;

	

}
//------------------------------------------------------------------------------------
CSceneBuilderApp::~CSceneBuilderApp()
{
// 	delete m_OgitorsRoot;
// 	delete m_System;
// 	delete m_OgreRoot;
}
//------------------------------------------------------------------------------------
BOOL CSceneBuilderApp::InitInstance()
{
	// InitCommonControlsEx() is required on Windows XP if an application
	// manifest specifies use of ComCtl32.dll version 6 or later to enable
	// visual styles.  Otherwise, any window creation will fail.
	try
	{
		m_OgitorsRoot = &Apo::ClientRoot::getInstance();
		INITCOMMONCONTROLSEX InitCtrls;
		InitCtrls.dwSize = sizeof(InitCtrls);
		// Set this to include all the common control classes you want to use
		// in your application.
		InitCtrls.dwICC = ICC_WIN95_CLASSES | ICC_DATE_CLASSES | ICC_COOL_CLASSES;
		InitCommonControlsEx(&InitCtrls);

		CWinAppEx::InitInstance();

		m_pszAppName = _T("sceneEdit");
		// Change the registry key under which our settings are stored
		// TODO: You should modify this string to be something appropriate
		// such as the name of your company or organization
		SetRegistryKey(_T("sceneEdit"));

		InitContextMenuManager();

		InitKeyboardManager();

		InitTooltipManager();
		CMFCToolTipInfo ttParams;
		ttParams.m_bVislManagerTheme = TRUE;
		theApp.GetTooltipManager()->SetTooltipParams(AFX_TOOLTIP_TYPE_ALL,
			RUNTIME_CLASS(CMFCToolTipCtrl), &ttParams);

#ifndef DEBUG
		CSplashScreen Splash(NULL);
		Splash.Create(CSplashScreen::IDD,NULL);
		//Splash.ShowWindow(SW_SHOW);
		//Splash.UpdateWindow();
#endif	
 		m_System = new CWinMFCSystem();

		// To create the main window, this code creates a new frame window
		// object and then sets it as the application's main window object
 		CMainFrame* pFrame = new CMainFrame;
 		if (!pFrame)
 			return FALSE;
 		m_pMainWnd = pFrame;
 		// create and load the frame with its resources
 		DWORD style = WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE;
 		style &= ~WS_VISIBLE;
		if (pFrame->LoadFrame(IDR_MAINFRAME,style, NULL,NULL))
		{
			//GetKeyboardManager()->ResetAll();
			m_System->SetWindows(&(pFrame->m_wndProperties.m_wndPropList),&(pFrame->m_wndSceneView.m_wndSceneView));
			pFrame->getChildView()->SetupOgre();
			//m_OgreRoot->addFrameListener(&(pFrame->getChildView()));
			// The one and only window has been initialized, so show and update it
			pFrame->ShowWindow(SW_SHOW);
			pFrame->UpdateWindow();
#ifndef DEBUG
			Splash.DestroyWindow();
#endif	
			pFrame->RedrawWindow();
		}

	}
	catch (...)
	{
		MessageBoxA(NULL,"An exception has occurred!" /*e.getFullDescription().c_str()*/, "An exception has occurred!", MB_ICONERROR | MB_TASKMODAL);
	}
	return TRUE;
}
//------------------------------------------------------------------------------------
void CSceneBuilderApp::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}

// CSceneBuilderApp customization load/save methods

void CSceneBuilderApp::PreLoadState()
{
	BOOL bNameValid;
	CString strName;
	bNameValid = strName.LoadString(IDS_EDIT_MENU);
	ASSERT(bNameValid);
	GetContextMenuManager()->AddMenu(strName, IDR_POPUP_EDIT);
	bNameValid = strName.LoadString(IDS_EXPLORER);
	ASSERT(bNameValid);
	GetContextMenuManager()->AddMenu(strName, IDR_POPUP_EXPLORER);
	GetContextMenuManager()->AddMenu(_T("User Defined Properties"), IDR_USERDEFINEDPROPERTIES);
}

void CSceneBuilderApp::LoadCustomState()
{
	m_FPSID = GetInt(_T("OgreFPS"), 4);
	if(m_FPSID > 11) m_FPSID = 4;
	m_FPSTimer = FPSLIST[m_FPSID];
}
//------------------------------------------------------------------------------------
void CSceneBuilderApp::SaveCustomState()
{
}
//------------------------------------------------------------------------------------
// CSceneBuilderApp message handlers
//------------------------------------------------------------------------------------
BOOL CSceneBuilderApp::OnIdle(LONG lCount)
{
	// In this example, as in most applications, you should let the
	// base class CWinApp::OnIdle complete its processing before you
	// attempt any additional idle loop processing.
	if (CWinApp::OnIdle(lCount))
		return TRUE;   

	unsigned int curtime = timeGetTime();

	if(((CMainFrame *)GetMainWnd())->getChildView()->m_RenderStop)
	{
		// if(MessageBox(GetMainWnd()->GetSafeHwnd(),L"Render Device is Lost! Please click ok to continue..",L"Device Lost",MB_OK) == IDOK)
		{
			((CMainFrame *)GetMainWnd())->getChildView()->m_RenderStop = false;
		}
	}

	HWND hWnd = GetForegroundWindow();

	if(GetMainWnd()->GetSafeHwnd() == hWnd || GetMainWnd()->GetForegroundWindow()->GetSafeHwnd() == hWnd)
	{
		//ViewKeyboard[VK_MENU] = (GetAsyncKeyState(VK_MENU) && 0xF000);
		if((curtime - m_LastTime) > m_FPSTimer)
		{
			m_LastTime = curtime;
			SendMessage(((CMainFrame *)GetMainWnd())->getChildView()->GetSafeHwnd(),WM_UPDATEOGRE,0,0);
		}
		else
		{
			Sleep(1);
		}

		((CMainFrame *)GetMainWnd())->getChildView()->ProcessKeyActions();
		
		return TRUE;
	}
	else 
		return FALSE;
}
//------------------------------------------------------------------------------------


